package com.kevin.j2eeToolBox.algorithm;

import org.junit.Test;

public class BinarySearch {

	@Test
	public void test() {

		int[] a = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

		for (int i = 0; i < 15; i++) {
			System.out.println(search(a, i));
		}
	}

	public int search(int[] a, int value) {

		int m = 0;
		int n = a.length - 1;

		int index;

		while (m <= n) {

			index = (m + n) / 2;

			if (value == a[index]) {
				return index;
			}

			if (value > a[index]) {
				m = index + 1;
			} else {
				n = index - 1;
			}
		}

		return -1;
	}

}
